/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgamesgui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import olympicgameslogic.*;

/**
 *
 * @author fabio mendonca
 */
public class DialogManageAppUser extends JFrame {

   private JTextField txtuser = new JTextField(15);
   private JTextField txtpass = new JTextField(15);
   private JComboBox comboTipo = new JComboBox(AppUser.userType.values());
   private JButton btnew;
   private AppUser appU;
   private ImageIcon img;
   private String tit;
   private Boolean trig = false;
   ClassLoader cldr = this.getClass().getClassLoader();
   DialogAppUsers da;

   public DialogManageAppUser(String titulo, AppUser b, DialogAppUsers da) {
      super(titulo);
      super.setIconImage(Toolkit.getDefaultToolkit().getImage(cldr.getResource("images/extra/icon.png")));
      appU = b;
      this.da = da;


   }

   public void showJan() {
      if (appU == null) {
	 img = new ImageIcon(cldr.getResource("images/extra/user_add.png"));
	 tit = "Add User";
      } else {
	 img = new ImageIcon(cldr.getResource("images/extra/user_edit.png"));
	 tit = "Edit User";
      }
      setLayout(new BorderLayout());
      setBackground(Color.white);
      setVisible(true);

//      Criação da barra de topo
      JPanel pannorte = new JPanel(new FlowLayout(FlowLayout.CENTER));
      pannorte.setSize(200, 60);
      pannorte.setBackground(Color.white);
      JLabel background = new JLabel(tit);
      background.setFont(new Font("Arial", Font.BOLD, 14));
      background.setIcon(img);
      pannorte.add(background);

// criação dos campos             
      JPanel pancentro = new JPanel();
      pancentro.setBackground(Color.white);
      pancentro.setLayout(new GridLayout(3, 1));

// tipo de utilizador        
      JPanel p0 = new JPanel(new FlowLayout(FlowLayout.LEFT));
      p0.add(new JLabel("Perfil:"));
      comboTipo.setSelectedIndex(-1);
      p0.add(comboTipo);
      p0.setBackground(Color.white);
      pancentro.add(p0);
//campo username        
      JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
      p1.add(new JLabel("Username:"));
//        txtuser.setText(OlympicGamesGui.ap.getUtilizador().getUsername().toString());
      p1.add(txtuser);
      p1.setBackground(Color.white);
      pancentro.add(p1);
//Campo password        
      JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
      p2.add(new JLabel("Password:"));
      p2.add(txtpass);
//        txtpass.setText(OlympicGamesGui.ap.getUtilizador().getPassword().toString());
      btnew = new JButton("Generate");
      btnew.setVisible(true);
      btnew.setFont(new Font("Arial", Font.PLAIN, 8));
      p2.add(btnew);
      p2.setBackground(Color.white);
      pancentro.add(p2);

      if (appU == null) {
	 comboTipo.setSelectedItem(0);
	 txtuser.setText("");
	 txtpass.setText("");

      } else {
	 comboTipo.setSelectedItem(appU.getPerfil());
	 txtuser.setText(appU.getUsername());
	 txtpass.setText(appU.getPassword());
      }

//painel com botoes        
      JPanel pansul = new JPanel();
      pansul.setSize(200, 20);
      pansul.setBackground(Color.white);
      JButton btok = new JButton("OK", new ImageIcon(cldr.getResource("images/extra/btok.png")));
      pansul.setLayout(new FlowLayout(FlowLayout.CENTER));
      pansul.add(btok);


      add(pannorte, BorderLayout.NORTH);
      add(pancentro, BorderLayout.CENTER);
      add(pansul, BorderLayout.SOUTH);
      setSize(340, 180);
      setResizable(false);
      setLocationRelativeTo(null);
      setVisible(true);
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);

      btnew.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    String t = "";
	    for (int i = 0; i < 5; i++) {
	       int j = (int) (1 + (Math.random() * 9));
	       t = t + j;
	    }
	    txtpass.setText(t);
	    JOptionPane.showMessageDialog(DialogManageAppUser.this, "New Password Created: " + txtpass.getText());
	 }
      });

      btok.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
            OlympicGamesGui.ap.getAppUsers().add(new AppUser(txtuser.getText(),txtpass.getText(),(AppUser.userType)comboTipo.getSelectedItem()));                                                                               
	    da.refreshTable();
	    dispose();
	 }
      });
   }
}
